// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    active: 0,
    content: 0,
    num: 12,
    phone: "",
    current_user: {
      username: ""
    },
    list1: [{
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon1.png",
        title: "待付款"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon2.png",
        title: "待分享"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon3.png",
        title: "待发货"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon4.png",
        title: "待收货"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon5.png",
        title: "待评价"
      }
    ],
    list2: [{
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon6.png",
        title: "优惠券"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon7.png",
        title: "商品收藏"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon8.png",
        title: "历史浏览"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon9.png",
        title: "退货售后"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon10.png",
        title: "我的评价"
      }
    ],
    settings: [{
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon11.png",
        title: "新人红包"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon12.png",
        title: "多多果园"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon13.png",
        title: "砍价免费拿"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon14.png",
        title: "天天领现金"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon15.png",
        title: "多多爱消除"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon16.png",
        title: "多多牧场"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon17.png",
        title: "多多短视频"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon18.png",
        title: "火车票"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon19.png",
        title: "收货地址"
      },
      {
        src: "cloud://renqingwu-a7ukj.7265-renqingwu-a7ukj-1303048163/user/icon20.png",
        title: "官方客服"
      }
    ]
  },
  //点击后将切换到登录界面
  changePage() {
    var index = this.data.index;
    index = (index + 1) % 2;
    this.setData({
      index: index
    });
  },
  //输入位数不满足手机11位则发送验证码按钮显示为不可点击
  verify(e) {
    if (this.data.content == 1) {
      return;
    }
    if (e.detail.value.length == 11) {
      this.setData({
        active: 1,
        phone: e.detail.value
      });
    } else {
      this.setData({
        active: 0
      });
    }
  },
  send_code() {
    if (this.data.active == 1) {
      this.setData({
        active: 0,
        content: 1
      });
      var that = this;
      var num = this.data.num;
      var phone = this.data.phone;
      var timer = setInterval(() => {
        if (num == 0) {
          clearInterval(timer);
          that.setData({
            content: 0,
            num: 12
          });
        } else {
          that.setData({
            num: --num
          });
        }

      }, 1000);
      const db = wx.cloud.database();
      db.collection("users").where({
        username: phone
      }).get({
        success: function (res) {
          if (res.data.length == 0) {
            wx.showToast({
              title: '未查询到此用户',
              icon: 'loading',
              duration: 2000
            });
          } else {
            wx.showToast({
              title: '验证码:' + res.data[0].code,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    }
  },
  submit(e) {
    console.log(e.detail.value.phone_number, e.detail.value.code);
    var that = this;
    if (e.detail.value.phone_number.length != 0 && e.detail.value.code.length != 0) {
      const db = wx.cloud.database();
      db.collection("users").where({
        username: e.detail.value.phone_number,
        code: e.detail.value.code
      }).get({
        success: function (res) {
          if (res.data.length == 0) {
            wx.showToast({
              title: '手机号或验证码错误',
              icon: 'none',
              duration: 2000
            });
          } else {
            wx.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                db.collection('users').doc('44e883245f6b00b9015166a06534bafc').update({
                  // data 传入需要局部更新的数据
                  data: {
                    // 表示将 done 字段置为 true
                    index: e.detail.value.phone_number
                  },
                  success: function (res) {
                    db.collection('users').where({
                        username: e.detail.value.phone_number
                      })
                      .get({
                        success: function (res) {
                          // res.data 是包含以上定义的两条记录的数组
                          that.setData({
                            current_user: res.data[0]
                          });
                          console.log(pages.length);
                          
                        }
                      })
                  }
                });
                that.setData({
                  index: 2
                });
              }
            });

          }
        }
      });
    } else {
      wx.showToast({
        title: '请输入手机号和验证码',
        icon: 'none',
        duration: 2000
      })
    }
  },
  logout() {
    var that = this;
    wx.showModal({
      title: '退出登录',
      content: '确定退出登录吗？',
      success(res) {
        if (res.confirm) {
          const db = wx.cloud.database();
          db.collection('users').doc('44e883245f6b00b9015166a06534bafc').update({
            // data 传入需要局部更新的数据
            data: {
              // 表示将 done 字段置为 true
              index: ""
            },
            success: function (res) {
              that.setData({
                index: 0,
                current_user: {
                  username: ""
                }
              });
            }
          });
        }
      }
    });


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const db = wx.cloud.database()
    var that = this;
    db.collection('users').doc('44e883245f6b00b9015166a06534bafc').get({
      success: function (res) {
        // res.data 包含该记录的数据
        var username = res.data.index;
        db.collection('users').where({
            username: username
          })
          .get({
            success: function (res) {
              // res.data 是包含以上定义的两条记录的数组
              that.setData({
                current_user: res.data[0]
              });
            }
          });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})